package com.qxzn.app.personalcenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.LoginAndRegisterModel;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.CommonUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 忘记密码
 */
public class ForgetPwdActivity extends BaseActivity implements MyInterface.GetCodeListener {

    @BindView(R2.id.et_phone)
    EditText etPhone;
    private LoginAndRegisterModel loginAndRegisterModel;
    private String phone;
    private int type = 2;

    @Override
    protected int initLayout() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        tv_title.setVisibility(View.GONE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        loginAndRegisterModel = new LoginAndRegisterModel(this);
        loginAndRegisterModel.setGetCodeListener(this);
    }

    @OnClick({R2.id.tv_get_code})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_get_code) {
            phone = etPhone.getText().toString().trim();
            if (TextUtils.isEmpty(phone)) {
                AppUtil.showShortToast(this, etPhone.getHint().toString());
                return;
            }
            if (!CommonUtil.isMobile(phone)) {
                AppUtil.showShortToast(this, "手机号码不正确，请重新输入");
                return;
            }
            JSONObject params = new JSONObject();
            params.put("phone", phone);
            params.put("type", type);
            showProgress();
            loginAndRegisterModel.getCode(params);
        }
    }

    @Override
    public void onGetCodeSuccess(BaseBean baseBean) {
        hideProgress();
        if (baseBean != null) {
            if (baseBean.getCode() == 200) {
                AppUtil.showToast(this, "验证码发送成功");
                startActivity(new Intent(this, RegisterCodeActivity.class).putExtra("phone", phone).putExtra("type", type));
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            AppUtil.showToast(this, baseBean.getMsg());
        } else {
            AppUtil.showToast(this, "验证码发送失败，请重试");
        }
    }

    @Override
    public void onGetCodeFailure() {
        hideProgress();
        AppUtil.showToast(this, "网络错误，请稍后再试！");
    }

    @Override
    protected void onDestroy() {
        if (loginAndRegisterModel != null) {
            loginAndRegisterModel.removeListener();
        }
        super.onDestroy();
    }
}
